﻿using AutoMapper;
using B.S.Filtration.Domain.到货检验;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Read.Api.Application.Command.到货检验.QualityReportWorkCommand;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Read.Api.Application.CommandHander.到货检验.QualityReportWorkHandler
{
    ///FTQualityReportWorkHandler：这是一个处理 FTQualityReportWorkCommand 请求的类，继承自 IRequestHandler 接口。
    public class FTQualityReportWorkHandler:IRequestHandler<FTQualityReportWorkCommand,APIResult<List<QualityReportWork>>>
    {
        //ISqlSugarClient：这是一个用于数据库操作的客户端，提供了对数据库的访问和操作功能。
        private readonly ISqlSugarClient db;
        //IMapper：这是 AutoMapper 库 的核心接口，用于对象映射。
        private readonly IMapper mapper;
        //构造函数：FTQualityReportWorkHandler，接收两个参数 db 和 mapper。
        public FTQualityReportWorkHandler(ISqlSugarClient db, IMapper mapper)
        {
            //将传入的 db 和 mapper 参数赋值给类的私有字段。
            this.db = db;
            //将传入的 mapper 参数赋值给类的私有字段。
            this.mapper = mapper;
        }

        public async Task<APIResult<List<QualityReportWork>>> Handle(FTQualityReportWorkCommand request, CancellationToken cancellationToken)
        {
            //创建一个APIResult对象，类型为List<QualityReportWork>
            APIResult<List<QualityReportWork>> result = new APIResult<List<QualityReportWork>>();
            //使用SqlSugar查询QualityReportWork表中IsDel为false且QualityReportWorkid等于请求参数中的QualityReportWorkid的记录
            var list = await db.Queryable<QualityReportWork>().Where(x => x.IsDel == false && x.QualityReportWorkid == request.QualityReportWorkid).ToListAsync();
            //将查询结果赋值给result的data属性
            result.data = list;
            //将result的code属性设置为success
            result.code = APIResultCode.success;
            //将result的msg属性设置为"显示成功"
            result.msg = "显示成功";
            //返回result对象
            return await Task.FromResult(result);
        }
    }
}
